﻿using Infrastructure.kflwyg;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure
{
    public class ConfigEnvironment
    {
        public static readonly ConfigEnvironment Instance = new ConfigEnvironment();

        private string appID = "kf03d7968c70a13dd0";

        private string appSecret = "7151b78ec79ad371db476d3e600de5fe";

        private string serverHost = "http://kf-jianguan.ttbiji.com/";

        public ConfigEnvironment()
        {

        }

        #region 开封用工平台

        /// <summary>
        /// 平台网址
        /// </summary>
        public string ServerHost
        {
            get
            {
                return serverHost;
            }
        }

        /// <summary>
        /// 项目ID(AppID) 
        /// </summary>
        public string AppID
        {
            get
            {
                return appID;
            }
        }


        /// <summary>
        /// 项目密钥(AppSecret)
        /// </summary>
        public string AppSecret
        {
            get
            {
                return appSecret;
            }
        }

        private long kflwyg_access_token_expire = 0;

        private int kflwyg_access_token_error_count = 0;

        private string kflwyg__access_token = "";

        /// <summary>
        /// 开封劳务用工
        /// </summary>
        public string KFLWYG_ACCESS_TOKEN
        {
            get
            {
                if (this.kflwyg_access_token_expire < DateTime.Now.Ticks)
                {
                    string appid = ConfigEnvironment.Instance.AppID;
                    string appsecret = ConfigEnvironment.Instance.AppSecret;// WowSecret;

                    var url = $"{ServerHost}/api/1.0/service_join/refresh_access_token";
                    var postData = new { app_id = appid, app_secret = appsecret };

                    try
                    {
                        string responseStr = new WebApiHelper().Post(url, JsonConvert.SerializeObject(postData));
                        ResultData tokenObj = JsonConvert.DeserializeObject<ResultData>(responseStr);
                        if (tokenObj.code == 200)
                        {
                            kflwyg__access_token = tokenObj.data.access_token;
                            kflwyg_access_token_expire = DateTime.Now.AddHours(1).Ticks;//一个小时刷一次
                        }
                        else
                        {
                            if (kflwyg_access_token_error_count > 5)
                            {
                                throw new Exception("获取 XIAOSK ACCESS TOKEN 失败");
                            }
                            kflwyg_access_token_error_count++;
                            kflwyg_access_token_expire = 0;
                            return KFLWYG_ACCESS_TOKEN;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (kflwyg_access_token_error_count > 5)
                        {
                            throw new Exception("获取 XIAOSK ACCESS TOKEN 失败");
                        }
                        kflwyg_access_token_error_count++;
                        kflwyg_access_token_expire = 0;
                        return KFLWYG_ACCESS_TOKEN;
                    }
                }
                kflwyg_access_token_error_count = 0;
                return kflwyg__access_token;
            }
        }


        #endregion
    }
}
